package com.adams.action.videoupload;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.Reader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.adams.access.db.bean.ActionType;
import com.adams.custom.page.core.BusinessDealFlowContext;
import com.adams.custom.page.core.service.BusinessDealFlowEngineService;
import com.adams.custom.page.core.service.DicTableService;
import com.adams.utils.SpringBeanFactoryUtil;

public class UploadReceiveServlet extends HttpServlet
{

	private Logger log = Logger.getLogger(UploadReceiveServlet.class);

	private DicTableService dicTableService = (DicTableService) SpringBeanFactoryUtil
			.getBean("dicTableService");

	private BusinessDealFlowEngineService businessDealFlowEngineService = (BusinessDealFlowEngineService) SpringBeanFactoryUtil
			.getBean("businessDealFlowEngineService");

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * Constructor of the object.
	 */
	public UploadReceiveServlet()
	{
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy()
	{
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{
		log.info("doGet invokin.....");
		this.doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to
	 * post.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	@SuppressWarnings("unchecked")
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{
		log.info("doPost invokin.....");

		response.setContentType("text/xml");
		PrintWriter out = response.getWriter();
		try
		{
			StringBuilder sb = new StringBuilder();
			char[] cbuf = new char[1024];
			Reader reader = new InputStreamReader(request.getInputStream(),
					"UTF-8");

			int count = 0;
			while (true)
			{
				count = reader.read(cbuf);
				if (count == -1)
				{
					break;
				}
				sb.append(cbuf, 0, count);
			}

			UploadVideoResource video = DataTransformer.toObject(sb.toString());

			// ServletContext application = request.getSession()
			// .getServletContext();
			// ApplicationContext context = WebApplicationContextUtils
			// .getRequiredWebApplicationContext(application);
			//
			// UploadVideoService uploadVideoService = (UploadVideoService)
			// context
			// .getBean("uploadVideoService");
			Map inputMap = new HashMap();
			inputMap.put("id", video.getId());
			inputMap = dicTableService.getDataByKey(
					UploadVideoResource.TABLECODE, inputMap);
			// UploadVideoResource targetVideo = uploadVideoService.get(video
			// .getId());
			// targetVideo.setCode(video.getCode());
			inputMap.put("code", video.getCode());
			List<Map> inputMaps = new ArrayList<Map>();
			inputMaps.add(inputMap);
			BusinessDealFlowContext context = new BusinessDealFlowContext(
					UploadVideoResource.TABLECODE, ActionType.ACTIONTYPE_EDIT,
					inputMaps);
			businessDealFlowEngineService.run(context);

			// uploadVideoService.update(targetVideo);

			log.info("###########=[[" + Result.SUCCESS.toXml() + "]]");

			out.println(Result.SUCCESS.toXml());
		} catch (Exception ex)
		{
			ex.printStackTrace();
			out.println(Result.FAIL.toXml());
		}
		out.flush();
		out.close();
	}

	/**
	 * Initialization of the servlet. <br>
	 * 
	 * @throws ServletException
	 *             if an error occurs
	 */
	public void init() throws ServletException
	{

	}

}
